package com.jinghua.treatment.service.impl;

import com.jinghua.treatment.entity.*;
import com.jinghua.treatment.mapper.SysInquiryAnswersSubMapper;
import com.jinghua.treatment.mapper.SysInquirySubjectCategoryItemMapper;
import com.jinghua.treatment.mapper.SysInquirySubjectCategoryMapper;
import com.jinghua.treatment.service.SysInquirySubjectCategoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 王兴光 on 2018/5/25.
 */
@Service
public class SysInquirySubjectCategoryServiceImpl implements SysInquirySubjectCategoryService {
    @Resource
    private SysInquirySubjectCategoryMapper inquirySubjectCategoryMapper;
    @Resource
    private SysInquirySubjectCategoryItemMapper inquirySubjectCategoryItemMapper;
    @Resource
    private SysInquiryAnswersSubMapper inquiryAnswersSubMapper;

    @Override
    public List<SysInquirySubjectCategory> selectByExample(SysInquirySubjectCategoryExample example) {
        return inquirySubjectCategoryMapper.selectByExample(example);
    }

    @Override
    public List<SysInquirySubjectCategory> selectByAnswers(List<String> answers) {
        return inquirySubjectCategoryMapper.selectByAnswers(answers);
    }

    @Override
    public SysInquirySubjectCategory selectByPrimaryKey(Integer categoryId) {
        return null;
    }


    @Override
    public List<SysInquirySubjectCategory> selectTemplateByInquiryId(String inquiryId) {
        // 定义一级分类列表对象
        List<SysInquirySubjectCategory> level1List = new ArrayList<SysInquirySubjectCategory>();
        // 迭代一级分类列表
        for (SysInquirySubjectCategory level1 : inquirySubjectCategoryMapper.selectLevel1ByInquiryId(inquiryId)) {
            level1.setLevel2List(new ArrayList<SysInquirySubjectCategory>());
            // 根据一级分类编号获取二级分类列表
            for (SysInquirySubjectCategory level2 : inquirySubjectCategoryMapper.selectLevel2ByTemplate(level1.getCategoryCode())){
                level1.getLevel2List().add(level2); // 添加二级分类
                level2.setLevel3List(new ArrayList<SysInquirySubjectCategoryItem>());
                // 根据二级分类编号获取三级分类列表
                for(SysInquirySubjectCategoryItem level3 : inquirySubjectCategoryItemMapper.selectLevel3ByTamplate(level2.getCategoryCode())){
                    level2.getLevel3List().add(level3); // 添加三级分类
                }
            }
            level1List.add(level1); // 添加一级分类
        }
        return level1List;
    }

    @Override
    public List<SysInquiryAnswersSub> selectSubListByInquiryId(String inquiryId) {
        // 定义查询条件
        SysInquiryAnswersSubExample example = new SysInquiryAnswersSubExample();
        SysInquiryAnswersSubExample.Criteria criteria = example.createCriteria();
        example.getOredCriteria().add(criteria.andRecordIdEqualTo(inquiryId));

        // 根据问诊单编号，获取问诊单记录详情列表
        List<SysInquiryAnswersSub> subList = inquiryAnswersSubMapper.selectByExample(example);
        return subList;
    }
}
